<template>
  <div v-loading="model.loading">
    <div class="d-flex justify-content-between align-items-center">
      <div>
        <el-button size="medium" type="primary" @click="editor">写文章</el-button>
      </div>
      <div class="d-flex">
        <el-input size="medium" placeholder="请输入内容" prefix-icon="el-icon-search" 
          class="mr-3" v-model="keyword">
        </el-input>
        <el-button type="primary" size="medium" icon="el-icon-search" @click="newSearch">搜索</el-button>
      </div>
    </div>
    <el-table :data="this.model.rows" class="mt-3" border>
      <el-table-column type="index" label="序号" width="50">
    </el-table-column>
      <el-table-column prop="title" label="标题" :show-overflow-tooltip="true">
      </el-table-column>
      <el-table-column prop="abstract" label="简介" :show-overflow-tooltip="true">
      </el-table-column>
      <el-table-column prop="viewCount" label="浏览量" width="100" align="center">
      </el-table-column>
      <el-table-column prop="likeCount" label="点赞量" width="100" align="center">
      </el-table-column>
      <el-table-column label="更新时间" width="160" align="center">
        <template slot-scope="scope">
          {{ $formatDateTime(scope.row.updateDate) }}
        </template>
      </el-table-column>
      <el-table-column prop="updateDate" label="操作" align="center" width="120">
        <template slot-scope="scope">
          <el-button type="text" @click="editorRow(scope.row)">编辑</el-button>
          <el-button type="text" @click="deleteRow(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      class="mt-2 text-right"
      @current-change="handleCurrentChange"
      :current-page.sync="model.pageNo"
      :page-size="model.pageSize"
      layout="total, prev, pager, next"
      :total="model.total"
      :hide-on-single-page="model.total <= 10">
    </el-pagination>
  </div>
</template>
<script>
import api from '@/api'
import PagedList from '@/libs/paged-list.js'
export default {
  title: "文章列表",
  created() {
    this.newSearch()
  },
  data() {
    return {
      model: new PagedList(api.article),
      loading: false,
      keyword: '',
    }
  },
  methods: {
    newSearch() {
      // console.log('asdfasdfasdfasdfasd');
      this.model.search()
      // this.loading = true
      // this.$get(api.article).then(res => {
      //   this.articles = res.data.data
      //   this.loading = false
      // }).catch(() => {
      //   this.loading = false
      // })
    },
    editor() {
      this.$router.push('/editor')
    },
    editorRow(row) {
      this.$router.push(`/editor/${row.id}`)
    },
    deleteRow(row) {
      this.$confirm(`确认删除"${row.title}"吗？`).then(() => {
        this.loading = true
        this.$delete(`${api.article}/${row.id}`).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.newSearch()
        }).catch(() => {
          this.loading = false
        })
      })
    },
    handleCurrentChange(index) {
      this.model.pageLoad(index);
    },
  }
}
</script>
<style lang="scss">
</style>